package com.dwd.servicemanager.service.impl;

import com.dwd.basic.service.impl.BaseServiceImpl;
import com.dwd.basic.utils.PageBean;
import com.dwd.servicemanager.mapper.ProductDetailMapper;
import com.dwd.servicemanager.mapper.ProductMapper;
import com.dwd.servicemanager.pojo.Product;
import com.dwd.servicemanager.pojo.ProductDetail;
import com.dwd.servicemanager.pojo.dto.ProductDto;
import com.dwd.servicemanager.query.ProductQuery;
import com.dwd.servicemanager.service.IProductService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Description: TODO
 * @Author 汶狄
 * @Date 2021/1/21 15:23
 * @Version 1.0
 */
@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductDetailMapper productDetailMapper;

    /**
     * @param product
     */
    @Override
    public void save(Product product) {
        //存储产品
        super.save(product);
        //存储详情
        ProductDetail productDetail = product.getProductDetail();
        //设置产品
        productDetail.setProduct(product);
        //存储详情
        productDetailMapper.save(productDetail);
    }

    /**
     * 修改
     *
     * @param product
     */
    @Override
    public void update(Product product) {
        super.update(product);
        ProductDetail productDetail = product.getProductDetail();
        productDetail.setProduct(product);
        productDetailMapper.update(productDetail);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long id) {
        //删除服务
        super.delete(id);
        //删除服务详情
        productDetailMapper.deleteByProductId(id);
    }

    /**
     * 单个上架
     *
     * @param id
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void up(Long id) {
        productMapper.up(id);
    }

    /**
     * 单个下架
     *
     * @param id
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void down(Long id) {
        productMapper.down(id);
    }

    /**
     * 批量下架
     *
     * @param products
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchDown(List<Product> products) {
        productMapper.batchDown(products);
    }


    /**
     * 批量删除
     *
     * @param products
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchDelete(List<Product> products) {
        //删除产品
        productMapper.batchDelete(products);
        //删除产品详情
        productDetailMapper.batchDelete(products);
    }

    @Override
    public PageBean<Product> queryPageSalecount(ProductQuery productQuery) {
        //总条数
        Long count = productMapper.queryCount(productQuery);
        if (count == 0) {
            return new PageBean<Product>();
        }
        List<Product> departments = productMapper.queryPageSalecount(productQuery);
        return new PageBean<Product>(count, departments);
    }

    @Override
    public PageBean<Product> queryPageSaleprice(ProductQuery productQuery) {
        //总条数
        Long count = productMapper.queryCount(productQuery);
        if (count == 0) {
            return new PageBean<Product>();
        }
        List<Product> departments = productMapper.queryPageSaleprice(productQuery);
        return new PageBean<Product>(count, departments);
    }
}
